home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / sviluppo / svilupp2 / gmsppr10.lha / HappyBlanker.c < prev    next >
C/C++ Source or Header  |  1996-10-15  |  5KB  |  200 lines

  1. #ifndef DOS_DOS_H
  2. #include <dos/dos.h>
  3. #endif
  4.  
  5. #ifndef DEVICES_INPUT_H
  6. #include <devices/input.h>
  7. #endif
  8.  
  9. #include <proto/alib.h>
  10. #include <proto/exec.h>
  11.  
  12. #include "Global.h"
  13.  
  14. /************************************************************************/
  15.  
  16. #include "StaticSaveds.h"
  17.  
  18. /************************************************************************/
  19.  
  20. static struct Task *HappyBlankerTask;
  21.  
  22. static struct Task *Parent;    /* acknowledge */
  23. static ULONG AckSignal;        /* acknowledge */
  24.  
  25. struct SignalSemaphore HappyBlankerSemaphore;
  26. struct SignalSemaphore HappyBlankerSemaphore2;
  27.  
  28. static ULONG UserCount;
  29.  
  30. /************************************************************************/
  31.  
  32. STATIC_SAVEDS(void,HappyBlankerCode)
  33.  
  34. {
  35.   struct MsgPort MsgPort;
  36.   struct timerequest TimerRequest;
  37.  
  38.   ObtainSemaphore(&HappyBlankerSemaphore2);
  39.  
  40.   MsgPort.mp_SigTask=FindTask(NULL);
  41.   MsgPort.mp_Flags=PA_SIGNAL;
  42.   MsgPort.mp_SigBit=SIGBREAKB_CTRL_F;
  43.   MsgPort.mp_MsgList.lh_Head=(struct Node *)&MsgPort.mp_MsgList.lh_Tail;
  44.   MsgPort.mp_MsgList.lh_Tail=NULL;
  45.   MsgPort.mp_MsgList.lh_TailPred=(struct Node *)&MsgPort.mp_MsgList.lh_Head;
  46.  
  47.   TimerRequest.tr_node.io_Message.mn_ReplyPort=&MsgPort;
  48.   if (OpenDevice("timer.device",UNIT_VBLANK,&TimerRequest.tr_node,0)==0)
  49.     {
  50.       struct IOStdReq InputRequest;
  51.  
  52.       InputRequest.io_Message.mn_ReplyPort=&MsgPort;
  53.       if (OpenDevice("input.device",0,&InputRequest,0)==0)
  54.     {
  55.       int Done;
  56.  
  57.       Signal(Parent,AckSignal);
  58.       Permit();
  59.       ReplyMsg(&TimerRequest.tr_node.io_Message);
  60.       Done=FALSE;
  61.       do
  62.         {
  63.           struct Message *Message;
  64.  
  65.           if (Wait(SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_F) & SIGBREAKF_CTRL_C)
  66.         {
  67.           Done=TRUE;
  68.         }
  69.           else if ((Message=GetMsg(&MsgPort))!=NULL)
  70.         {
  71.           struct InputEvent InputEvent;
  72.  
  73.           assert(Message==&TimerRequest.tr_node.io_Message);
  74.           InputRequest.io_Command=IND_WRITEEVENT;
  75.           InputRequest.io_Data=&InputEvent;
  76.           InputRequest.io_Length=sizeof(InputEvent);
  77.           InputEvent.ie_NextEvent=NULL;
  78.           InputEvent.ie_Class=IECLASS_RAWKEY;
  79.           InputEvent.ie_SubClass=0;
  80.           InputEvent.ie_Code=0x6f;
  81.           InputEvent.ie_Qualifier=0;
  82.           InputEvent.ie_X=0;
  83.           InputEvent.ie_Y=0;
  84.           DoIO(&InputRequest);
  85.           TimerRequest.tr_node.io_Command=TR_ADDREQUEST;
  86.           TimerRequest.tr_time.tv_secs=30;
  87.           TimerRequest.tr_time.tv_micro=0;
  88.           SendIO(&TimerRequest.tr_node);
  89.         }
  90.         }
  91.       while (!Done);
  92.       AbortIO(&TimerRequest.tr_node);
  93.       WaitIO(&TimerRequest.tr_node);
  94.       CloseDevice(&InputRequest);
  95.     }
  96.       CloseDevice(&TimerRequest.tr_node);
  97.     }
  98.   Forbid();
  99.   HappyBlankerTask=NULL;
  100.   ReleaseSemaphore(&HappyBlankerSemaphore2);
  101. }
  102.  
  103. /****** gamesupport.library/GS_HappyBlanker ******************************
  104. *
  105. *    NAME
  106. *    GS_HappyBlanker -- keep a screenblanker happy
  107. *
  108. *    SYNOPSIS
  109. *    Success = GS_HappyBlanker()
  110. *       d0
  111. *
  112. *    ULONG GS_HappyBlanker(void);
  113. *
  114. *    FUNCTION
  115. *    Turn the happy blanker on.
  116. *    This means that input events will be sent down the input stream
  117. *    to make sure that a screenblanker doesn't suddenly blank the
  118. *    screen.
  119. *    Turn this on while playing. Turn it off in pause or demo mode.
  120. *
  121. *    RESULT
  122. *
  123. *    SEE ALSO
  124. *    GS_NoHappyBlanker()
  125. *
  126. *************************************************************************/
  127.  
  128. SAVEDS(ULONG,LibGS_HappyBlanker)
  129.  
  130. {
  131.   ULONG RC;
  132.  
  133.   ObtainSemaphore(&HappyBlankerSemaphore);
  134.   if (UserCount==0)
  135.     {
  136.       BYTE TheSignal;
  137.  
  138.       RC=FALSE;
  139.       if ((TheSignal=AllocSignal(-1))!=-1)
  140.     {
  141.       AckSignal=(1<<TheSignal);
  142.       SetSignal(0,AckSignal);
  143.       Parent=FindTask(NULL);
  144.       if ((HappyBlankerTask=CreateTask("gamesupport.library: Happy blanker",0,HappyBlankerCode,4096))!=NULL)
  145.         {
  146.           Wait(AckSignal);
  147.           if (HappyBlankerTask!=NULL)
  148.         {
  149.           UserCount++;
  150.           RC=TRUE;
  151.         }
  152.         }
  153.       FreeSignal(TheSignal);
  154.     }
  155.     }
  156.   else
  157.     {
  158.       UserCount++;
  159.       RC=TRUE;
  160.     }
  161.   ReleaseSemaphore(&HappyBlankerSemaphore);
  162.   return RC;
  163. }
  164.  
  165. /****** gamesupport.library/GS_NoHappyBlanker ****************************
  166. *
  167. *   NAME
  168. *    GS_NoHappyBlanker -- turn the happy blanker off
  169. *
  170. *   SYNOPSIS
  171. *    GS_NoHappyBlanker()
  172. *
  173. *    void GS_NoHappyBlanker(void);
  174. *
  175. *   FUNCTION
  176. *    Turn off the happy blanker.
  177. *
  178. *   NOTE
  179. *    Only call this if GS_HappyBlanker() returned success.
  180. *
  181. *   SEE ALSO
  182. *    GS_HappyBlanker()
  183. *
  184. *************************************************************************/
  185.  
  186. SAVEDS(void,LibGS_NoHappyBlanker)
  187.  
  188. {
  189.   ObtainSemaphore(&HappyBlankerSemaphore);
  190.   if (--UserCount==0)
  191.     {
  192.       assert(HappyBlankerTask!=NULL);
  193.       Signal(HappyBlankerTask,SIGBREAKF_CTRL_C);
  194.       ObtainSemaphore(&HappyBlankerSemaphore2);
  195.       ReleaseSemaphore(&HappyBlankerSemaphore2);
  196.       HappyBlankerTask=NULL;
  197.     }
  198.   ReleaseSemaphore(&HappyBlankerSemaphore);
  199. }
  200.